package cc.iotkit.screen.controller;

import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.screen.service.BizReportService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author chenx
 * @date 2024年07月22日
 */
@Api(tags = {"大屏展示"})
@Slf4j
@RestController
@RequestMapping("/report")
public class BizReportController {

	@Autowired
	private BizReportService bizReportService;


	@ApiOperation("围栏外告警")
	//@SaCheckPermission("iot:report:alertrecord")
	@PostMapping("/alertrecord")
	public List<Map<String,Object>> alertrecord() {
		return bizReportService.alertrecord();
	}

	@ApiOperation("设备状态展示")
	//@SaCheckPermission("iot:report:deviceStatus")
	@PostMapping("/deviceStatus")
	public List<Map<String,Object>> deviceStatus() {
		return bizReportService.deviceStatus();
	}

	@ApiOperation("区域设备汇总-省级")
	//@SaCheckPermission("iot:report:areadevice")
	@PostMapping("/areadevice")
	public List<Map<String,Object>> areaDevice() {
		return bizReportService.areaDevice();
	}

	@ApiOperation("区域设备汇总-市级")
	//@SaCheckPermission("iot:report:areacitydevice")
	@PostMapping("/areacitydevice")
	public List<Map<String,Object>> areaCityDevice(@Validated @RequestBody String data) {
		Long param = 0L;
		if(!StringUtils.isEmpty(data)){
			String idStr = JSONUtil.parseObj(data).get("data").toString();
			if(!StringUtils.isEmpty(idStr)){
				String id = JSONUtil.parseObj(idStr).get("data").toString();
				if(!StringUtils.isEmpty(id)){
					param = Long.parseLong(id);
				}
			}
		}
		return bizReportService.areaCityDevice(param);
	}

	@ApiOperation("数量排名（省份）")
	//@SaCheckPermission("iot:report:sfnumranking")
	@PostMapping("/sfnumranking")
	public List<Map<String,Object>> sfnumRanking() {
		return bizReportService.sfnumRanking();
	}

	@ApiOperation("工单次数排名（省份）")
	//@SaCheckPermission("iot:report:sfworkranking")
	@PostMapping("/workranking")
	public List<Map<String,Object>> sfWorkRanking() {
		return bizReportService.sfWorkRanking();
	}

	@ApiOperation("数量排名（客户）")
	//@SaCheckPermission("iot:report:khnumranking")
	@PostMapping("/khnumranking")
	public List<Map<String,Object>> khnumRanking() {
		return bizReportService.khnumRanking();
	}

	@ApiOperation("工单次数排名（客户）")
	//@SaCheckPermission("iot:report:khworkranking")
	@PostMapping("/khworkranking")
	public List<Map<String,Object>> khworkRanking() {
		return bizReportService.khworkRanking();
	}

	@ApiOperation("客户工单月度统计")
	//@SaCheckPermission("iot:report:khworkorder")
	@PostMapping("/khworkorder")
	public List<Map<String,Object>> khWorkOrder(@Validated @RequestBody String tenantName) {
		String param = "";
		if(!StringUtils.isEmpty(tenantName)){
			String data = JSONUtil.parseObj(tenantName).get("data").toString();
			if(!StringUtils.isEmpty(data)){
				param = JSONUtil.parseObj(data).get("tenantName").toString();
			}
		}
		return bizReportService.khWorkOrder(param);
	}


	@ApiOperation("设备租赁数量")
	//@SaCheckPermission("iot:report:devicelease")
	@PostMapping("/devicelease")
	public List<Map<String,Object>> deviceLease(@Validated @RequestBody String queryType) {
		String param = "1";
		if(queryType!=null){
			String data = JSONUtil.parseObj(queryType).get("data").toString();
			if(!StringUtils.isEmpty(data)){
				param = JSONUtil.parseObj(data).get("queryType").toString();
			}
		}
		return bizReportService.deviceLease(Integer.parseInt(param));
	}

	@ApiOperation("产品类型月度统计")
	//@SaCheckPermission("iot:report:producttype")
	@PostMapping("/producttype")
	public List<Map<String,List<Map<String,Object>>>> productType() {
		return bizReportService.productType();
	}

	@ApiOperation("出租时长分类统计")
	//@SaCheckPermission("iot:report:leaseduration")
	@PostMapping("/leaseduration")
	public List<Map<String,Object>> leaseDuration() {
		return bizReportService.leaseDuration();
	}

	@ApiOperation("设备状态-2")
	//@SaCheckPermission("iot:report:devicestatus2")
	@PostMapping("/devicestatus2")
	public List<Map<String,Object>> deviceStatus2() {
		return bizReportService.deviceStatus2();
	}


	@ApiOperation("告警统计")
	//@SaCheckPermission("iot:report:alertstatistics")
	@PostMapping("/alertstatistics")
	public List<Map<String,Object>> alertStatistics(@Validated @RequestBody String queryType) {
		String param = "1";
		if(queryType!=null){
			String data = JSONUtil.parseObj(queryType).get("data").toString();
			if(!StringUtils.isEmpty(data)){
				param = JSONUtil.parseObj(data).get("queryType").toString();
			}
		}
		return bizReportService.alertStatistics(Integer.parseInt(param));
	}

	@ApiOperation("工单统计")
	//@SaCheckPermission("iot:report:workstatistics")
	@PostMapping("/workstatistics")
	public List<Map<String,List<Map<String,Object>>>> workStatistics(@Validated @RequestBody String queryType) {
		String param = "1";
		if(queryType!=null){
			String data = JSONUtil.parseObj(queryType).get("data").toString();
			if(!StringUtils.isEmpty(data)){
				param = JSONUtil.parseObj(data).get("queryType").toString();
			}
		}
		return bizReportService.workStatistics(Integer.parseInt(param));
	}

	@ApiOperation("保养提醒")
	//@SaCheckPermission("iot:report:maintenancereminder")
	@PostMapping("/maintenancereminder")
	public List<Map<String,Object>> maintenanceReminder() {
		return bizReportService.maintenanceReminder();
	}

}
